package pl.pkolodziej.flex.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pl.pkolodziej.flex.dao.UserCourseDAO;
import pl.pkolodziej.platform.vo.course.Course;
import pl.pkolodziej.platform.vo.course.UserCourseInfo;
import pl.pkolodziej.platform.vo.user.UserInfo;

@Service
@RemotingDestination(value="flexService")
public class FlexServiceImpl implements FlexService {
	
	@Autowired
	public UserCourseDAO userCourseDAO;
	
	private static final Logger logger = Logger.getLogger(FlexServiceImpl.class);
	
	@Transactional
	@RemotingInclude
	public String updateCourse(String username,int course_id,int pages,int score, String data) {
		userCourseDAO.updateUserCourseScoreAndPage(username, course_id, pages, score);
		return "course saved";
	}
	
	@Transactional
	@RemotingInclude
	public String updateStartTest(String username,int course_id,int pages,int score, String data) {
		userCourseDAO.updateUserCourseStartTest(username, course_id, score,data);
		logger.info("CONVERSA UPDATE START TEST " + username + " : " + course_id);
		return "start test saved";
	}
	
	@Transactional
	@RemotingInclude
	public String updateEndTest(String username,int course_id,int pages,int score, String data) {
		userCourseDAO.updateUserCourseEndTest(username, course_id, score, data);
		return "end test saved";
	}
	@Transactional
	@RemotingInclude
	public String startTestDone(String username, int course_id){
		userCourseDAO.startTestDone(username, course_id);
		logger.info("CONVERSA START TEST DONE " + username + " : " + course_id);
		return "start test saved";
	}
	@Transactional
	@RemotingInclude
	public String endTestDone(String username, int course_id){
		userCourseDAO.endTestDone(username, course_id);
		logger.info("CONVERSA END TEST DONE " + username + " : " + course_id);
		return "end test saved";
	}
	
	@Transactional
	@RemotingInclude
	public String getCourseInfo(String username, int course_id){
		Course c = userCourseDAO.getCourse(course_id);
		UserInfo ui = userCourseDAO.getUserInfo(username);
		UserCourseInfo uci = userCourseDAO.getUserCourseInfo(username, course_id);
		return uci.getStarttest_state()+"|"+uci.getStarttest()+"|"+ui.getBirthdate()+"|"+ui.getCity()+"|"+c.getTitle()+"|"+c.getCoach();
	}
}
